package cn.com.zetatech.loader.common.ftp;

import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;


public class ConnectionClientFactory implements PooledObjectFactory<ConnectionClient> {

    private final String serverIP;
    private final int serverPort;
    private final String userName;
    private final String password;
    private final String type;

    public ConnectionClientFactory(String serverIP, int serverPort, String userName, String password, String type) {
        this.serverIP = serverIP;
        this.serverPort = serverPort;
        this.userName = userName;
        this.password = password;
        this.type = type;
    }

    @Override
    public void destroyObject(PooledObject<ConnectionClient> p) throws Exception {
        p.getObject().disconnect();
    }

    @Override
    public PooledObject<ConnectionClient> makeObject() throws Exception {
        return new DefaultPooledObject<>(ConnectionClient.getClient(serverIP, serverPort, userName, password, type));
    }

    @Override
    public boolean validateObject(PooledObject<ConnectionClient> p) {
        return p.getObject().isConnected();
    }

    @Override
    public void activateObject(PooledObject<ConnectionClient> p) throws Exception {

    }

    @Override
    public void passivateObject(PooledObject<ConnectionClient> p) throws Exception {

    }


}
